/* api */
import { getQualityConfig, getQualityConfigGray } from '@src/api/ProductV2Api'
/* enum */
import ConfigCodeEnum from '@model/enum/ConfigCodeEnum'
/* hooks */
import { useFetchResult } from '@hooks/useFetch'
/* model */
import { getProductSettingQualitySearchModel, ProductSettingQualityGraySearchModel } from '@model/param/in/Setting'
import RootWindowInitData from '@model/interface/initData/RootWindowInitData'
import { SettingProductQualityGrayResult, SettingProductQualityResult } from '@model/param/out/Setting'
/* vue */
import { computed, ComputedRef, onMounted } from 'vue'
/* util */
import { getRootWindowInitData } from '@src/util/window'

const initData: RootWindowInitData = getRootWindowInitData()

export function useProductSetting(): [ComputedRef<boolean>] {
  // 产品设置质保功能返回值
  let settingProductQualityResult: ComputedRef<SettingProductQualityResult> = useProductSettingQualityFetch()
  // 质保功能是否开启
  let isOpenQuality: ComputedRef<boolean> = computed(() => Boolean(settingProductQualityResult?.value?.QUALITY_PLEDGE))
  
  // 产品设置质保功能是否显示返回值
  // let settingProductQualityGrayResult: ComputedRef<SettingProductQualityGrayResult> = useProductSettingQualityGrayFetch()
  // 质保功能灰度是否开启
  // let isOpenGrayQuality: ComputedRef<boolean> = computed(() => Boolean(settingProductQualityGrayResult?.value?.QUALITY_PLEDGE))
  
  return [isOpenQuality]
}

export function useProductSettingQualityFetch(): ComputedRef<SettingProductQualityResult> {
  
  const params: ComputedRef<getProductSettingQualitySearchModel> = useProductSettingQualityFetchParams()
  
  let [data, callback] = useFetchResult<getProductSettingQualitySearchModel, SettingProductQualityResult>(async () => {
    return getQualityConfig(params.value)
  }, params.value)
  
  onMounted(() => {
    callback()
  })
  
  return computed(() => data.value || {})
}

export function useProductSettingQualityFetchParams(): ComputedRef<getProductSettingQualitySearchModel> {  
  return computed(() => ({ tenantId: initData?.user?.tenantId || '', configCode: [ConfigCodeEnum.Quality] }))
}

// export function useProductSettingQualityGrayFetch(): ComputedRef<SettingProductQualityGrayResult> {
  
//   const params: ComputedRef<ProductSettingQualityGraySearchModel> = useProductSettingQualityGrayFetchParams()
  
//   let [data, callback] = useFetchResult<ProductSettingQualityGraySearchModel, SettingProductQualityGrayResult>(async () => {
//     return getQualityConfigGray(params.value)
//   }, params.value)
  
//   onMounted(() => {
//     callback()
//   })
  
//   return computed(() => data.value || {})
// }

// export function useProductSettingQualityGrayFetchParams(): ComputedRef<ProductSettingQualityGraySearchModel> {  
//   return computed(() => ({ code: ConfigCodeEnum.Quality }))
// }
